package com.example.jcpmpose

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel

@Composable
fun WellnessTaskItem(
    modifier: Modifier = Modifier,
    task: WellnessTask,
    onCheckedChange: (WellnessTask, Boolean) -> Unit = { task, checked -> },
    onClose: (WellnessTask) -> Unit = {}
) {
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically
    ) {

        Text(
            text = task.label,
            modifier = Modifier
                .weight(1.0f)
                .padding(start = 16.dp)
        )
        Checkbox(
            checked = task.checked,
            onCheckedChange = {
                onCheckedChange(task, it)
            }
        )

        IconButton(onClick = { onClose(task) }) {
            Icon(
                imageVector = Icons.Filled.Close,
                contentDescription = "Close"
            )
        }

    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
@Preview(showBackground = true)
fun WellnessTaskList(
    modifier: Modifier = Modifier,
    model: WellnessViewModel = WellnessViewModel()
) {
    LazyColumn(
        modifier = modifier
    ) {
        items(model.tasks, key = { it.id }) {

            WellnessTaskItem(
                modifier = Modifier.animateItemPlacement(),
                task = it,
                onClose = { task ->
                    model.remove(task)
                },
                onCheckedChange = { task, checked ->
                    model.changeTaskChecked(task, checked)
                }
            )
        }
    }

}

class WellnessTask(
    val id: Int,
    val label: String,
    initialChecked: Boolean = false
) {
    var checked by mutableStateOf(initialChecked)
}

class WellnessViewModel : ViewModel() {
    private val _tasks = getWellnessTasks().toMutableStateList()

    val tasks get() = _tasks

    fun remove(item: WellnessTask) = tasks.remove(item)

    fun changeTaskChecked(item: WellnessTask, checked: Boolean) =
        tasks
            .find { it.id == item.id }
            ?.let { it.checked = checked }
}

fun getWellnessTasks() = List(30) { i -> WellnessTask(i, "Task # $i") }

